﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace DataAccessApplicationBlockGettingStarted
{
    public partial class Form1 : Form
    {
        private GettingStartedBusinessLogic buslogic = null;

        public Form1()
        {
            InitializeComponent();

            buslogic = new GettingStartedBusinessLogic();
            buslogic.Initialize();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            buslogic.Dispose();
            buslogic = null;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.customerBindingSource.DataSource = buslogic.GetCustomers();
        }

        private void customerDataGrid_Click(object sender, EventArgs e)
        {
            // Show the dialog. Set the bindingSource on the dialog to be the bindingSource
            // on this form. That way the edit dialog will operate on the Current record
            Cursor.Current = Cursors.WaitCursor;
            using (var dlg = new EditCustomerDialog())
            {
                // Position bindingsource of the dialog at the correct position
                dlg.customerBindingSource.DataSource = this.customerBindingSource;
                dlg.customerBindingSource.Position = this.customerBindingSource.Position;
                Cursor.Current = Cursors.Default;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    // User clicked Save, so now we must save the changed Customer record to the database
                    buslogic.SaveCustomer((Customer)customerBindingSource.Current);
                }
            }
        }
    }
}